/* promise 形式getsetting */
const getSetting=()=>{
  
    return new Promise((resolve, reject) => {
      wx.getSetting({
          success: (result)=>{
            resolve(result)
          },
          fail: (err)=>{
            reject(err)
          },
          complete: ()=>{}
      })
  })
}

/* promise 形式chooseAddress */
const chooseAddress=()=>{
  
    return new Promise((resolve, reject) => {
      wx.chooseAddress({
          success: (result)=>{
            resolve(result)
          },
          fail: (err)=>{
            reject(err)
          },
          complete: ()=>{}
      })
  })
}

/* promise 形式openSetting */
const openSetting=()=>{
  
    return new Promise((resolve, reject) => {
      wx.openSetting({
          success: (result)=>{
            resolve(result)
          },
          fail: (err)=>{
            reject(err)
          },
          complete: ()=>{}
      })
  })
}

/* promise 形式login */
const login=()=>{
  
  return new Promise((resolve, reject) => {
    wx.login({
        timeout:10000,
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
})
}

/* promise 形式requestPayment 

   {obj} pay 支付所必要的参数
*/
const requestPayment=(pay)=>{
  
  return new Promise((resolve, reject) => {
    wx.requestPayment({
        ...pay,
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
})
}

/* promise 形式showModal */
const showModal=({content})=>{
  //console.log(content)
  return new Promise((resolve, reject) => {
    wx.showModal({
      title: '提示',
      content: content,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
  })
}

/* promise 形式showToast */
const showToast=({title,icon})=>{
  //console.log(content)
  return new Promise((resolve, reject) => {
    wx.showToast({
        title: title,
        icon: icon,
        image: '',
        duration: 1500,
        mask: false,
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
  })
}


module.exports = {
    openSetting: openSetting,
    chooseAddress: chooseAddress,
    getSetting: getSetting,
    showModal:showModal,
    showToast:showToast,
    login:login,
    requestPayment:requestPayment,
  }

